/**
 * @Project: Runer
 * @Title: ActivitySearchPerson.java
 * @Package com.runlooker.runer.contact
 * @author town
 * @date 2014-5-29 下午8:14:57
 * @Copyright: 2014 www.yineng.com.cn Inc. All rights reserved.
 * @version V1.0
 */
package com.runlooker.runer.ui.searchPerson;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.runlooker.runer.R;
import com.runlooker.runer.application.MyApplication;
import com.runlooker.runer.service.XmppService;
import com.runlooker.runer.ui.base.BaseActivity;
import com.runlooker.runer.ui.personInfo.MyInfoActivity;
import com.runlooker.runer.ui.personInfo.PersonInfoActivity;
import com.runlooker.runer.util.NetworkUtil;
import com.runlooker.runer.util.T;

/**
 * @ClassName ActivitySearchPerson
 * @Description 模块描述
 * @author town
 * @Date 2014-5-29
 */
public class SearchPersonActivity extends BaseActivity {
	private final String tag = SearchPersonActivity.class.getSimpleName();
	private EditText mInputAcountET;
	private static final int SUCCESS = 0;
	private static final int FAILED = 1;
	private static final int EXCEPTION = 2;
	private Button mSearchBT;
	private String mSearchedAccount;
	private XmppService mXmppService;
	private Thread mThread;
	private String mAccount;

	private final Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case SUCCESS: // 服务器联系人搜索成功
				Intent intent;
				if (MyApplication.isSelf(SearchPersonActivity.this, mAccount)) {
					intent = new Intent(SearchPersonActivity.this,
							MyInfoActivity.class);
				} else {
					intent = new Intent(SearchPersonActivity.this,
							PersonInfoActivity.class);
					intent.putExtra("account", mAccount);
				}
				startActivity(intent);
				finish();
				break;
			case FAILED: // 服务器没有指定的联系人
				T.showShort(SearchPersonActivity.this, "未查询到帐号");
				break;
			case EXCEPTION: // 搜索失败，如网络断开等等
				break;
			default:
				break;
			}
		}
	};

	ServiceConnection mConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			mXmppService = ((XmppService.XMPPBinder) service).getService();
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			mXmppService = null;
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_searchfriend);
		initialize();
		initEvent();
	}

	private void initialize() {
		Intent intent = new Intent(this, XmppService.class);
		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
		mInputAcountET = (EditText) findViewById(R.id.searchperson_input_et);
		mSearchBT = (Button) findViewById(R.id.searchperson_search_btn);
	}

	private void initEvent() {
		mSearchBT.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mSearchedAccount = mInputAcountET.getText().toString().trim();
				if (!(NetworkUtil.getNetworkState(SearchPersonActivity.this) == NetworkUtil.NETWORN_NONE)) {
					startSearchThread(mSearchedAccount);
				} else {
					T.showShort(SearchPersonActivity.this, "无法连接网络");
				}
			}
		});
	}

	private void startSearchThread(final String account) {
		if (mThread == null) {
			mThread = new Thread(new Runnable() {
				public void run() {
					Message msg = Message.obtain();
					if (mXmppService != null && mXmppService.isLogin()) {
						try {
							String acont = mXmppService.searchAccount(account);
							if (acont != null && acont.equals(account)) {
								msg.what = SUCCESS;
								mAccount = account;
							} else {
								msg.what = FAILED;
							}
						} catch (Exception e) {
							msg.what = EXCEPTION;
						} finally {
							handler.sendMessage(msg);
						}
						if (mThread != null) {
							synchronized (mThread) {
								mThread = null;
							}
						}
					}
				}
			});
			mThread.start();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unbindService(mConnection);
		handler.removeCallbacksAndMessages(null);
	}

	public void back(View view) {
		finish();
	}
}
